package com.leo;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @ClassName ListViewDemo
 * @Description 拖拽排序
 * @Author wangss
 * @date 2020.06.13 15:19
 * @Version 1.0
 */
public class ListViewDragSort extends Application {

    private int index = 0;

    private String data = "";

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        Button sort = new Button("排序");

        ListView<String> listView = new ListView();
        listView.setPrefWidth(400d);
        listView.setPrefHeight(400d);

        ObservableList<String> obsList = listView.getItems();
        obsList.add("AAAAAAAA");
        obsList.add("BBBBBBBB");
        obsList.add("CCCCCCCC");
        obsList.add("DDDDDDDD");
        obsList.add("EEEEEEEE");
        obsList.add("FFFFFFFF");

        anchorPane.getChildren().addAll(sort, listView);
        AnchorPane.setTopAnchor(sort, 100d);
        AnchorPane.setLeftAnchor(sort, 100d);
        AnchorPane.setTopAnchor(listView, 140d);
        AnchorPane.setLeftAnchor(listView, 100d);

        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.setTitle("搜索功能");
        primaryStage.setResizable(false);
        primaryStage.show();

        listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {

            int position = 0;

            @Override
            public ListCell<String> call(ListView<String> param) {

                Label label = new Label();
                label.setPrefHeight(20d);
                label.setFont(new Font(15));

                ListCell<String> listCell = new ListCell<String>() {
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);

                        if (!empty) {
                            label.setText(item);
                            this.setGraphic(label);
                        }
                    }
                };

                // 拖动进入
                listCell.setOnDragEntered(event -> {
                    position = param.getItems().indexOf(label.getText());
                    param.getFocusModel().focus(position);
                });

                // 拖动检测
                listCell.setOnDragDetected(event -> {
                    Dragboard dragboard = listCell.startDragAndDrop(TransferMode.COPY);

                    Text text = new Text(data);
                    text.setFont(new Font(30));
                    text.setFill(Paint.valueOf("#121FEE"));
                    WritableImage writableImage = new WritableImage((int) param.getPrefWidth() - 40, 40);
                    text.snapshot(new SnapshotParameters(), writableImage);
                    dragboard.setDragView(writableImage, writableImage.getWidth() / 2, writableImage.getHeight() / 2);

                    ClipboardContent content = new ClipboardContent();
                    content.putString(data);
                    dragboard.setContent(content);

                });

                // 拖动经过
                listCell.setOnDragOver(event -> {
                    event.acceptTransferModes(TransferMode.COPY);
                });

                // 拖动释放
                listCell.setOnDragDropped(event -> {
//                    String str = data;

                    String str = event.getDragboard().getString();

                    if (position == -1) {
                        position = param.getItems().size() - 1;
                    }

                    param.getItems().remove(index);

                    param.getItems().add(position, str);

                    param.getSelectionModel().select(position);
                });

                return listCell;
            }
        });

        listView.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {
            index = newValue.intValue();
        });
        listView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            data = newValue;
        });
    }
}
